package web.common;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: duanbin
 * Date: 2007-8-22
 * Time: 10:11:04
 * To change this template use File | Settings | File Templates.
 */
public class XmlAccessUtil {
    protected final static Logger logger = Logger.getLogger(XmlAccessUtil.class);
    public final static String DELIMIT = ",";

    public static Document getXmlDoc(String fileName) {
        try {
            SAXReader saxReader = new SAXReader();
            String feature = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
            saxReader.setFeature(feature, false);
            return saxReader.read(new File(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void modifyXmlDoc(Document document, String fileName) {
        try {
            OutputFormat outformat = new OutputFormat();
/*
            OutputFormat outformat = OutputFormat.createPrettyPrint();
            outformat.setTrimText(false);
            outformat.setNewlines(false);
            outformat.setIndent(false);
          outformat.setEncoding("UTF-8");
*/

            XMLWriter output = new XMLWriter(
                    new FileWriter(new File(fileName)), outformat);
            output.write(document);
            output.close();
            logger.info("Modified File: "  + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<String> getTokenValues(Document document, String _node) {
        Node node = document.selectSingleNode(_node);
        if(node!=null){
           return stringToken2List(node.getText(), DELIMIT);
        }
        return null;
    }

    public static List<String> stringToken2List(String input, String delemit) {
        StringTokenizer st = new StringTokenizer(input, delemit, false);
        List<String> list = new ArrayList<String>();
        while (st.hasMoreTokens()) {
            list.add(st.nextToken().trim());
        }
        return list;
    }
    public static String list2String(List list) {
        String ret = "\n";
        for (Object aList : list) {
            ret += (getIndent() + aList + "\n");
        }
        return ret;
    }
    private static String getIndent(){
        return "\t\t\t\t\t\t\t\t";
    }
    public static String getPath(String p){
        if(!p.endsWith("\\")){
           p+="\\";
        }
        return p;
    }
    public static void insertElementAt(Element orgElement, Element newElement, int index) {
        Element parent = orgElement.getParent();
        List list = parent.content();
        list.add(index, newElement);
    }

    public static void insertElementAfter(Element orgElement, Element newElement) {
        insertElementAt(orgElement, newElement, orgElement.getParent().indexOf(orgElement) + 1);
    }




}
